*! This file demonstrates how to conduct specification tests on various SF models.

*! Citation:
*! Wang, H.-J. and Ho, C.-W. (2010). "Estimating fixed-effect panel stochastic frontier
*! models by model transformation", Journal of Econometrics, 157(2), pp.286-296..



set trace off
myclear
set more off
set matsize 1000

capture log close
log  using demo_panel_log.txt, text replace


****************************************
use demo_panel.dta
****************************************


quie tab id, gen(dum) /* cnum is the id variable; this line generates firm dummies in dum1, dum2, ... */
drop dum1  /* drop one of the dummies to avoid multicollinearity */

global xvar x1 x2  /* save variable names in macro for easy reference */
global dvar dum*
global zvar z1



* ---- truncated normal model, using Green's dummy variable approach ------*

quie reg y $xvar $dvar  /* OLS regression, to get initial values */
mat b0 = e(b)           /* save the entire coefficient vector in a matrix */
mat b0a = b0[1,1..2]    /* save the first two coefficients (x1 and x2) in a matrix */

sfmodel y, prod dist(h) frontier($xvar $dvar) usigmas($zvar) vsigmas()
sf_init, frontier(b0) usigmas(-0.1 -0.1 ) vsigmas(-0.1)
sf_srch, n(2) nograph fast
ml max, diff gtol(1e-4) nrtol(1e-4)
sf_transform

mat b1 = e(b)
mat b1a = b1[1,"frontier:"]

sf_predict, jlms(jlms1) bc(bc1)

* ------- Scaling property model, using Greene's dummy varaible approach ---- *

sfmodel y, prod dist(t) scaling frontier($xvar $dvar) hscale($zvar) tau cu vsigmas()
sf_init, frontier(b1a) hscale(-0.1 ) tau(-0.1) cu(-0.1) vsigmas(-0.1)
sf_srch, n(2)  hscale($zvar) nograph fast
ml max, diff /* gtol(1e-4) nrtol(1e-4) */  nonrtolerance tol(1e-4)
sf_transform

sf_predict, jlms(jlms2) bc(bc2)


* --- Scaling property model, using Wang and Ho's model transformation approach (the best, haha) -------

sf_fixeff y, production dist(h) frontier($xvar) zvar($zvar) id(id) time(time) vsigmas usigmas
sf_init, frontier(b0a) zvar(0.1) vsigmas(-0.1) usigmas(-0.1)
sf_srch, frontier(x1 x2) zvar(z1 ) n(1) nograph fast
ml max, diff
sf_transform


sf_effindex, jlms(jlms3) bc(bc3)

 ****** compare ********

 corr jlms*

 corr bc*
